<script setup lang="ts">
import { User, Lock } from "@element-plus/icons-vue";
import { reactive, ref } from "vue";
import { ElMessage, ElNotification } from "element-plus";
import { useRouter, useRoute } from "vue-router";
//
import { getTime } from "@/utils/time";
//引入user仓库
import useUserStore from "@/store/modules/user";
const userStore = useUserStore();
const isRegister = ref(false);
const router = useRouter();
let $route = useRoute();
const form = ref();
const formModel = ref({
  username: "",
  password: "",
  repassword: "",
});

let formLogin = reactive({
  username: "admin",
  password: "111111",
});
const register = () => {
  //注册成功之前，先进行校验，校验成功->请求，校验失败->自动提示
  form.value.validate();
  ElMessage.success("注册成功");
  console.log("开始注册请求");
  isRegister.value = false;
};
const rules = {
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 5, max: 10, message: "用户名必须是5-10位 的字符", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    {
      pattern: /^\S{6,15}$/,
      message: "密码必须是6-15位 的非空字符",
      trigger: "blur",
    },
  ],
  repassword: [
    { required: true, message: "请输入密码", trigger: "blur" },
    {
      pattern: /^\S{6,15}$/,
      message: "密码必须是6-15位 的非空字符",
      trigger: "blur",
    },
    {
      validator: (rule: any, value: any, callback: any) => {
        //判断 value和当前form中收集的password是否一致
        if (value !== formModel.value.password) {
          callback(new Error("两次输入密码不一致"));
        } else {
          callback(); //就算校验成功，也需要callback
        }
      },
    },
  ],
};
const login = async () => {
  //登录成功之前，先进行校验，校验成功->请求，校验失败->自动提示
  try {
    await userStore.userLogin(formLogin);
    //登录成功
    let redirect: any = $route.query.redirect;

    router.push({ path: redirect || "/" });
    //
    ElNotification({
      type: "success",
      message: "欢迎回来",
      title: `Hi,${getTime()}好`,
    });
  } catch (error) {
    // ElNotification({
    //   type: "error",
    //   message: (error as Error).message,
    // });
    ElMessage.error((error as Error).message);
  }
};
</script>

<template>
  <div class="login">
    <iframe src="/html5/index.html" frameborder="0" width="100%" height="100%">
    </iframe>
    <div class="login-box">
      <el-form
        ref="form"
        size="large"
        autocomplete="off"
        v-if="isRegister"
        :model="formModel"
        :rules="rules"
      >
        <el-form-item class="el-center">
          <h1>注册</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            v-model="formModel.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formModel.password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            v-model="formModel.repassword"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入再次密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="register"
            class="button"
            type="primary"
            auto-insert-space
          >
            注册
          </el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="isRegister = false">
            ← 返回
          </el-link>
        </el-form-item>
      </el-form>
      <el-form
        ref="form"
        :rules="rules"
        :model="formLogin"
        size="large"
        autocomplete="off"
        v-else
      >
        <el-form-item class="el-center">
          <h1>登录</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            v-model="formLogin.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formLogin.password"
            :prefix-icon="Lock"
            placeholder="请输入密码"
            type="password"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item class="flex">
          <div class="flex">
            <el-checkbox>记住我</el-checkbox>
            <el-link type="primary" :underline="false" class="el-right"
              >忘记密码？</el-link
            >
          </div>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="login"
            class="button"
            type="primary"
            auto-insert-space
            >登录</el-button
          >
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="isRegister = true">
            注册 →
          </el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.button {
  width: 100%;
}
.login {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
}
.login-box {
  z-index: 999;
  display: flex;
  width: 440px;
  height: 400px;
  // margin-top: -25%;
  // margin-left: 75%;
  position: absolute;
  left: 80%;
  top: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 50px rgb(0 0 0 /10%);
  background: rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 10px;
}
.el-center {
  display: flex;
  justify-content: center;
  align-items: center;
}
h1 {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  height: 100%;
  width: 100%;
  font-size: 30px;
  font-weight: 600;
  color: #333;
}
.flex {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
// .el-input__wrapper {
//   width: 400px;
// }
:deep(.el-input__wrapper) {
  width: 400px;
}
</style>
